package com.example.smartcity1.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.smartcity1.R;
import com.example.smartcity1.bean.PatientListBean;
import com.example.smartcity1.service.patient.PatientClassListActivity;

import java.util.List;

public class PatientListAdapter extends ArrayAdapter<PatientListBean.RowsDTO> {

    private static final String TAG = "PatientListAdapter";

    public PatientListAdapter(@NonNull Context context, int resource, @NonNull List<PatientListBean.RowsDTO> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_patient_list, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.nameView = convertView.findViewById(R.id.nameView);
            viewHolder.sexView = convertView.findViewById(R.id.sexView);
            viewHolder.button = convertView.findViewById(R.id.button);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        PatientListBean.RowsDTO item = getItem(position);
        viewHolder.nameView.setText(item.getName());
        if (item.getSex() == null) {
            viewHolder.sexView.setText("");
        } else if (item.getSex() == 1) {
            viewHolder.sexView.setText("男");
        } else {
            viewHolder.sexView.setText("女");
        }
        if (item.getId() == null) {
            viewHolder.button.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.button.setVisibility(View.VISIBLE);
        }
        viewHolder.button.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), PatientClassListActivity.class);
            intent.putExtra("patientName",item.getName());
            getContext().startActivity(intent);
        });
        return convertView;
    }

    private class ViewHolder {
        TextView nameView;
        TextView sexView;
        ImageButton button;

    }
}
